#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
import sys

sys.path.append(
    os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "imlfapi")
)

import time
import threading
from db import GraphsTB


class Presstest(object):

    # def graph_info_by_model(self):
    #     res = GraphsTB.find_by_modle(username='郑水清', available=1)
    #     return [i.to_dict() for i in res]

    def graph_info_by_dbpool(self):
        return GraphsTB.find(username="郑水清")

    # def graph_info_by_dbpool(self):
    #     with connect_db('179') as db:
    #         res = db.select('graphs').where(username='郑水清').all()
    #         return res

    def testinterface(self):
        """压测接口"""
        global ERROR_NUM
        try:
            # self.graph_info_by_model()
            self.graph_info_by_dbpool()

        except Exception as e:
            print(e)
            ERROR_NUM += 1

    def testonework(self):
        """一次并发处理单个任务"""
        i = 0
        while i < ONE_WORKER_NUM:
            i += 1
            self.testinterface()
        time.sleep(LOOP_SLEEP)

    def run(self):
        """使用多线程进程并发测试"""
        t1 = time.time()
        Threads = []

        for i in range(THREAD_NUM):
            t = threading.Thread(target=self.testonework, name="T" + str(i))
            t.setDaemon(True)
            Threads.append(t)

        for t in Threads:
            t.start()
        for t in Threads:
            t.join()
        t2 = time.time()

        print("===============压测结果===================")
        print(
            "任务数量:"
            + str(THREAD_NUM)
            + "*"
            + str(ONE_WORKER_NUM)
            + "="
            + str(THREAD_NUM * ONE_WORKER_NUM)
        )
        print("总耗时(秒):" + str(t2 - t1))
        print("每次请求耗时(秒):" + str((t2 - t1) / (THREAD_NUM * ONE_WORKER_NUM)))
        print("每秒承载请求数:" + str(1 / ((t2 - t1) / (THREAD_NUM * ONE_WORKER_NUM))))
        print("错误数量:" + str(ERROR_NUM))


if __name__ == "__main__":
    THREAD_NUM = 8  # 并发线程总数
    ONE_WORKER_NUM = 20  # 每个线程的循环次数
    LOOP_SLEEP = 0.1  # 每次请求时间间隔(秒)
    ERROR_NUM = 0  # 出错数
    obj = Presstest()
    #  print(obj.farm_info_dbpool())
    #  print(obj.farm_info_algo_data())
    obj.run()


"""
# dbpoolpy 新版本debug=True测试结果
===============压测结果===================
任务数量:8*1000=8000
总耗时(秒):41.59695029258728
每次请求耗时(秒):0.00519961878657341
每秒承载请求数:192.32179147098742
错误数量:0
# dbpoolpy 新版本debug=False测试结果
===============压测结果===================
任务数量:8*1000=8000
总耗时(秒):39.572455644607544
每次请求耗时(秒):0.004946556955575943
每秒承载请求数:202.16081791452189
错误数量:0
# dbpoolpy 旧版本debug=True测试结果
===============压测结果===================
任务数量:8*1000=8000
总耗时(秒):37.955894470214844
每次请求耗时(秒):0.004744486808776855
每秒承载请求数:210.77095169705052
错误数量:0
# depoolpy 旧版本debug=False 测试结果
===============压测结果===================
任务数量:8*1000=8000
总耗时(秒):36.7275595664978
每次请求耗时(秒):0.004590944945812225
每秒承载请求数:217.82008100798106
错误数量:0


# sqlalchmy测试结果
===============压测结果===================
任务数量:8*1000=8000
总耗时(秒):40.6540207862854
每次请求耗时(秒):0.005081752598285675
每秒承载请求数:196.78250380338255
错误数量:0
"""
